<?php

namespace app\models;

use yii;
use yii\db\Query;

class Quote
{
    private $table;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . "quote";
    }

    public function one($field = '*', $where = [])
    {
        $db = new Query();
        $db->select($field)->from($this->table);
        if (!empty($where['id'])) $db->andWhere(['id' => $where['id']]);
        return $db->limit(1)->one();
    }

    public function add_quote($data)
    {
        $table = $this->table;
        return yii::$app->db->createCommand()->insert($table, $data)->execute();
    }

    /**报价size
     * @param array $where
     * @return int
     */
    public function get_quote_size($where = [])
    {
        $db = new Query();
        $db->select('count(distinct quote_id) as num')->from($this->table);
        if (!empty($where['job_number'])) $db->andWhere(['job_number' => $where['job_number']]);

        if (!empty($where['quote_id'])) $db->andWhere(['quote_id' => $where['quote_id']]);

        if (!empty($where['number'])) $db->andWhere(['number' => $where['number']]);
        if (!empty($where['customer_name'])) $db->andWhere(['like', 'customer_info', $where['customer_name']]);

        if (!empty($where['product'])) $db->andWhere(['like', 'product_list', $where['product']]);

        $re = $db->one();
        return $re ? $re['num'] : 0;
    }

    public function get_quote_list($p, $s, $where = [])
    {
        $db = new Query();
        $query = new Query();
        $query->select('max(id)')
            ->from($this->table);
        if (!empty($where['job_number'])) $query->andWhere(['job_number' => $where['job_number']]);

        if (!empty($where['quote_id'])) $query->andWhere(['quote_id' => $where['quote_id']]);

        if (!empty($where['number'])) $db->andWhere(['number' => $where['number']]);
        if (!empty($where['customer_name'])) $query->andWhere(['like', 'customer_info', $where['customer_name']]);

        if (!empty($where['product'])) $query->andWhere(['like', 'product_list', $where['product']]);

        $query->groupBy('quote_id');

        return $db->select('a.*,b.name')
            ->from($this->table . ' as a')
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . "staff as b", 'a.job_number=b.job_number')
            ->andWhere(['in', 'a.id', $query])
            ->orderBy(['a.date' => SORT_DESC, 'a.id' => SORT_DESC])
            ->offset(($p - 1) * $s)
            ->limit($s)
            ->all();
    }

    public function get_quote_by_id($id)
    {
        $db = new Query();
        return $db->select('a.*,b.email,b.en_name')
            ->from($this->table . " as a")
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . "staff as b", 'a.job_number=b.job_number')
            ->where(['a.id' => $id])
            ->one();
    }

    public function get_quote_id()
    {
        $db = new Query();
        return $db->select('count(distinct quote_id) as num')
            ->from(yii::$app->db->tablePrefix . 'quote')
            ->where(['date' => date('Y-m-d')])
            ->one();
    }

    public function get_quote_info_by_id($id)
    {
        $db = new Query();
        return $db->select('*')
            ->from(yii::$app->db->tablePrefix . 'quote')
            ->where(['id' => $id])
            ->one();
    }

    //通过报价编号得到最大版本号
    public function get_max_version_by_quote_num($quote_id)
    {
        $db = new Query();
        return $db->select('max(version) as version')
            ->from(yii::$app->db->tablePrefix . 'quote')
            ->where(['quote_id' => $quote_id])
            ->one();
    }

    //获取历史版本
    public function get_all_older_quote_list($where)
    {
        $db = new Query();
        $db->select('id,quote_id,version,create_at')
            ->from(yii::$app->db->tablePrefix . 'quote');
        if (!empty($where['job_number'])) {
            $db->where(['job_number' => $where['job_number']]);
        }
        if (!empty($where['quote_id'])) {
            $db->where(['quote_id' => $where['quote_id']]);
        }
        if (!empty($where['customer_name'])) {
            $db->where(['like', 'customer_info', $where['customer_name']]);
        }
        if (!empty($where['product'])) {
            $db->where(['like', 'product_list', $where['product']]);
        }

        return $db->orderBy(['quote_id' => SORT_DESC, 'create_at' => SORT_DESC])->all();
    }

    //所有报价商品
    public function get_all_quote_product($data = [])
    {
        $time = time() - 7 * 24 * 3600;
        $db = new Query();
        $db->select('*')
            ->from($this->table);

        if (!empty($data['enddate'])) {
            $db->andWhere();
        }
        if ($data['customer_name']) {
            $db->andWhere(['like', 'customer_info', $data['customer_name']]);
        }

        if ($data['date']) {
            $db->andWhere(['date' => $data['date']]);
        } else {
            $db->andWhere(['>=', 'create_at', $time]);
        }

        if ($data['product']) {
            $db->andWhere(['like', 'product_list', $data['product']]);
        }
        $list = $db->orderBy(['create_at' => SORT_DESC])
            ->all();
        return $list;
    }
}
